#include <stdio.h>
#include <string.h>

#include "security.h"
#include "security_error.h"

static char key[8] = {'1', '2', '3', '4', '5', '6', '7', '8'};

void demo_des_normal() {
        int ret = 0, i;
    char src1[] = "123";
    char src2[] = "12345678";

    char en[128] = { 0 };
    size_t en_len = 0;

    char de[128] = { 0 };
    size_t de_len = 0;

    ret = security_des_encrypt(en, &en_len, sizeof(en), src1, strlen(src1), key);
    printf("encrypt ret = 0x%04x\n", ret);
    printf("encode message hex:\n");
    for(i = 0; i < en_len; i++) {
        printf("%02x", (unsigned char)en[i]);
    }
    printf("\n");

    ret = security_des_decrypt(de, &de_len, sizeof(de), en, en_len, key);
    printf("decrypt ret = 0x%04x\n", ret);
    printf("decode message hex:\n");
    for(i = 0; i < de_len; i++) {
        printf("%02x ", (unsigned char)de[i]);
    }
    printf("\n");

    ret = security_des_encrypt(en, &en_len, sizeof(en), src2, strlen(src2), key);
    printf("encrypt ret = 0x%04x\n", ret);
    printf("encode message hex:\n");
    for(i = 0; i < en_len; i++) {
        printf("%02x", (unsigned char)en[i]);
    }
    printf("\n");

    ret = security_des_decrypt(de, &de_len, sizeof(de), en, en_len, key);
    printf("decrypt ret = 0x%04x\n", ret);
    printf("decode message hex:\n");
    for(i = 0; i < de_len; i++) {
        printf("%02x ", (unsigned char)de[i]);
    }
    printf("\n");
}

void demo_des_file() {
    FILE *fp = fopen("/tmp/src.txt", "w");
    char src[3] = {'1', '2', '3'};
    int ret = 0;

    fwrite(src, sizeof(char), sizeof(src), fp);
    fclose(fp);

    fp = fopen("/tmp/src2.txt", "w");
    fwrite(key, sizeof(char), sizeof(key), fp);
    fclose(fp);

    // ============================第一组加解密=====================================
    ret = security_des_file_encrypt("/tmp/src.txt", "/tmp/encrypt.txt", key);
    if(ret == SECURITY_OK) {
        printf("\033[32m");
    }
    else {
        printf("\033[33m");
    }

    printf("demo des encrypt file(/tmp/src.txt to /tmp/encrypt.txt) ret is 0x%04x\n", ret);

    printf("\033[0m");

    ret = security_des_file_decrypt("/tmp/encrypt.txt", "/tmp/decrypt.txt", key);
    if(ret == SECURITY_OK) {
        printf("\033[32m");
    }
    else {
        printf("\033[33m");
    }

    printf("demo des decrypt file(/tmp/encrypt.txt to /tmp/decrypt.txt) ret is 0x%04x\n", ret);

    printf("\033[0m");


    // ============================第二组加解密=====================================
    ret = security_des_file_encrypt("/tmp/src2.txt", "/tmp/encrypt2.txt", key);
    if(ret == SECURITY_OK) {
        printf("\033[32m");
    }
    else {
        printf("\033[33m");
    }

    printf("demo des encrypt file(/tmp/src2.txt to /tmp/encrypt2.txt) ret is 0x%04x\n", ret);

    printf("\033[0m");

    ret = security_des_file_decrypt("/tmp/encrypt2.txt", "/tmp/decrypt2.txt", key);
    if(ret == SECURITY_OK) {
        printf("\033[32m");
    }
    else {
        printf("\033[33m");
    }

    printf("demo des decrypt file(/tmp/encrypt2.txt to /tmp/decrypt2.txt) ret is 0x%04x\n", ret);

    printf("\033[0m");
}

int main() {

    demo_des_normal();
    demo_des_file();

    return 0;
}